<?php
    include_once("../action/checkAuthenticationAction.php");
    include_once("../service/userService.php");
    
    include_once("../util/authUtil.php");
    $perm_array = $_SESSION["permissions"];
?>
<!DOCTYPE html>
<html lang="en">
<head>
	<title>Dashboard</title>
	<?php include("import-css.php");?>	
    <style>
        #sortable1, #sortable2 { list-style-type: none; margin: 5px 0 0 0; padding: 0 0 2.5em; float: left; margin-right: 10px; border:1px solid black; min-height:20px; min-width:160px; border-radius:4px;}
        #sortable1 li, #sortable2 li {cursor:pointer; margin: 0 5px 5px 5px; padding: 5px; font-size: 1.2em; width: 120px; min-width:140px;}
    </style>
</head>
<body>
	<?php include("header.php");?>
    
    <div class="container-fluid">
		<div class="row-fluid">
            <!--Menu-->
            <?php include("menu.php");?>
            <?php
                $action = isset($_GET["action"]) ? $_GET["action"] : 8;
                if($action == 9){
                    $usergroup_id = $_GET["id"];
                    $edit_usergroup = getGroupDetail($usergroup_id);
                }                
                
            ?>
            <!--Content-->
            <?php
                if(!authUtil(array("edit staff","create staff","view staff","delete staff"),$perm_array,true)){
                    echo "<script>window.location.href='../admin/index.php';</script>";
                }
            ?>
            <div class="box span10 offset2">
                <div class="box-content">
                    <form class="form-horizontal" method="POST" action="<?php echo (($action == 8) ? "../action/newUserGroup.php" : "../action/editUserGroup.php"); ?>" id="new_usergroup_form">
                        <div class="control-group">
                            <label for="usergroupTitle" class="control-label">Nhóm nhân viên</label>
                            <div class="controls">
                                <input required type="text" value="<?php echo ($action == 9 ?  $edit_usergroup["ROLE_NAME"] : ""); ?>" id="usergroupTitle" name="usergroupTitle" class="input-xlarge focused">
                            </div>
                        </div>
						<!--PRIVILEGES-->
                        <!--
                        <div class="control-group">
                            <label class="control-label">Quyền</label>
                            <div class="controls" id="privilegeControlGroup">
                                <ul id="sortable1" class="connectedSortable">
                                        <?php
                                            if($action == 8){
                                                $privileges = getAllPrivilege();
                                            }else if($action == 9){
                                                $privileges = getAvailablePrivilegeOfUserGroup($edit_usergroup["ROLE_ID"]);
                                            }
                                            while($privilege = mysql_fetch_array($privileges)){
                                                echo "<li class='ui-state-default' value='".$privilege["PRI_ID"]."'>".$privilege["PRI_NAME"];
                                                echo "<input type='hidden' name='selectedPrivilegess[]' value='".$privilege["PRI_ID"]."'/>";
                                                echo "</li>";
                                            }
                                        ?>
                                </ul>
                                <div style="float:left;margin-right:5px;">===></div>
                                <ul id="sortable2" name="sortable2[]" class="connectedSortable">
                                    <?php
                                        if($action == 9){
                                            $privileges = getAllPrivilegeOfUserGroup($edit_usergroup["ROLE_ID"]);
                                        }
                                        while($privilege = mysql_fetch_array($privileges)){
                                            echo "<li class='ui-state-default'";
                                            echo " value='".$privilege["PRI_ID"]."'>".$privilege["PRI_NAME"];
                                            echo "<input type='hidden' name='selectedPrivilegess[]' value='".$privilege["PRI_ID"]."'/>";
                                            echo "</li>";
                                        }
                                    ?>                                    
                                </ul>
                            </div>
                        </div>
                        -->
                        <!--PRIVILEGES-->
                        <div class="control-group">
                            <label class="control-label" for="privileges_cbx">Quyền</label>
                            <div class="controls">
                                <select id="privileges_cbx" name="privileges_cbx[]" multiple data-rel="chosen">
                                    <?php
                                        if($action==9){
                                            $selectedPrivileges = getAllPrivilegeOfUserGroup($edit_usergroup["ROLE_ID"]);
                                            echo mysql_num_rows($selectedPrivileges);
                                            $availablePrivileges = getAvailablePrivilegeOfUserGroup($edit_usergroup["ROLE_ID"]);
                                            echo mysql_num_rows($availablePrivileges);    
                                        
                                        while($row = ConnectionManager::getInstance()->fetch($selectedPrivileges)){
                                    ?>
									       <option value="<?php echo $row["PRI_ID"];?>" selected><?php echo $row["PRI_NAME"];?></option>
                                    <?php
                                        }
                                        while($row = ConnectionManager::getInstance()->fetch($availablePrivileges)){
                                    ?>
                                            <option value="<?php echo $row["PRI_ID"];?>"><?php echo $row["PRI_NAME"];?></option>
                                    <?php
                                        }
                                        }else if($action==8){
                                            $privileges = getAllPrivilege();
                                            while($row = ConnectionManager::getInstance()->fetch($privileges)){
                                    ?>
                                                <option value="<?php echo $row["PRI_ID"];?>"><?php echo $row["PRI_NAME"];?></option>
                                    <?php
                                            }
                                        }
                                    ?>
                                </select>
                            </div>
                        </div>
                        <center>
                        <button type="submit" class="btn btn-primary"><?php echo (($action == 8) ? "Tạo mới" : "Cập nhật"); ?></button>
                        </center>
                        <input type="hidden" name="edit_usergroup_id" id="edit_usergroup_id" value="<?php echo isset($edit_usergroup["ROLE_ID"])?$edit_usergroup["ROLE_ID"]:""; ?>"/>
                        
                    </form>
                </div>
                
            </div><!--/span-->
        </div>
    </div>

    <?php include("footer.php");?>
		
	</div><!--/.fluid-container-->

	<?php include("import-js.php");?>
    <script>
    $(document).ready(function(){
        $("#new_usergroup_form").submit(function(e){
            e.preventDefault();
            var form = this;
			//Do not submit attributes and options in left-side list
            privileges1 = $("#sortable1").find("input");
            privileges1.each(function(){
                $(this).attr("name","");
            });
            form.submit();
        });
        
        $("#privileges_cbx_chzn").width("80%");
        $(".chzn-drop").width("80%");
    });
    $(function() {
		$( "#sortable1, #sortable2" ).sortable({
            connectWith: ".connectedSortable"
        });
    });
    
    </script>
</body>
</html>

